package com.xin.controller;
import com.xin.annoations.Permission;
import com.xin.constants.CommonResult;
import com.xin.service.RolePermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;

/**
 * 角色拥有的权限
 */
@RestController
@RequestMapping("/rolePermission")
@Api(tags = "角色拥有的权限")
@Permission(value = "控制角色权限信息",sn = "rolePermission")
public class RolePermissionController {
    @Resource
    private RolePermissionService rolePermissionService;

    /**
     * 查找角色所拥有的权限信息
     * @param roleId
     * @return
     */
    @ApiOperation("查找角色拥有的权限信息")
    @GetMapping("/findRoleHasPermission")
    @Permission(value = "查找角色拥有的权限信息",sn = "rolePermission:findRoleHasPermission")
    public CommonResult<?> findRoleHasPermission(Long roleId){
        return CommonResult.success(this.rolePermissionService.findRoleHasPermission(roleId),"请求成功");
    }

    /**
     * 修改角色的权限信息
     * @param roleId
     * @param sns
     * @return
     */
    @PostMapping("/updateRolePermission/{roleId}")
    @Permission(value = "修改角色的权限信息",sn = "rolePermission:updateRolePermission")
    @ApiOperation("修改角色的权限信息")
    public CommonResult<?> updateRolePermission(@PathVariable("roleId") Long roleId, @RequestBody List<String > sns){
           return this.rolePermissionService.updatePermissionToRole(sns,roleId)>0?CommonResult.success(null,"设置权限信息成功"):CommonResult.error(499,"网络异常");
    }
}
